package com.viphuli.fragment;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.OnClick;

import com.loopj.android.http.RequestParams;
import com.offroader.utils.OnlineParamUtils;
import com.offroader.utils.PhoneUtils;
import com.offroader.utils.ViewUtils;
import com.viphuli.app.R;
import com.viphuli.common.AccessTokenKeeper;
import com.viphuli.common.Constants;
import com.viphuli.common.MyPageHelper;
import com.viphuli.http.ApiRequest;
import com.viphuli.http.bean.part.AccountUser;
import com.viphuli.http.handler.MyBaseHttpResponseHandler.HandlerType;

/**
 * 我的
 * 
 * @author li.li
 *
 */
public class AccountFragment extends BaseProgressFragment implements SwipeRefreshLayout.OnRefreshListener {

	@Bind(R.id.id_account_user_name)
	protected TextView userNameText;

	@Bind(R.id.id_account_consume_ticket_count)
	protected TextView ticketCountText;

	@Bind(R.id.id_account_logout)
	protected ViewGroup logoutLayout;

	@Bind(R.id.id_account_service_tel)
	protected TextView serviceTelText;

	@Bind(R.id.id_account_invite)
	protected ViewGroup inviteLayout;

	private AccountUser user;

	@Override
	protected int obtainContentRes() {
		return R.layout.frag_account;
	}

	@Override
	protected void initParams() {
	}

	@Override
	protected void initView() {
		//标题
		caller.setTitle(getString(R.string.str_main_tab_name_my));
		//设置客服电话
		String sereviceTel = OnlineParamUtils.get(Constants.ONLINE_PARAMS_KEY_SERVICE_TEL);
		serviceTelText.setText(sereviceTel);

	}

	@Override
	public void onStart() {
		super.onStart();

		initRequest(HandlerType.loading);
	}

	@Override
	protected void initData() {
	}

	@Override
	public void onBackPressed() {
	}

	@Override
	protected void initRequest(HandlerType handlerType) {
		super.initRequest(handlerType);

		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());

		if (!tokenKeeper.isLogin()) {
			logoutLayout.setVisibility(View.GONE);
			ticketCountText.setVisibility(View.GONE);
			userNameText.setVisibility(View.VISIBLE);
			userNameText.setText(getString(R.string.str_account_bind_phone));
			userNameText.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View view) {
					//登陆
					AccountLoginFragment.go(getActivity(), Constants.ACTIVITY_REQUEST_REFREASH_CODE);
				}
			});

			//隐藏下拉刷新
			setRefreshing(false);

		} else {
			logoutLayout.setVisibility(View.VISIBLE);
			userNameText.setOnClickListener(null);

			//网络请求
			RequestParams params = new RequestParams();
			params.put("uid", tokenKeeper.getOpenId());
			ApiRequest.account.request(this, params, handlerType);
		}
	}

	@OnClick(R.id.id_account_gift_cash)
	public void giftCash() {

		AccessTokenKeeper token = AccessTokenKeeper.readAccessToken(getActivity());
		if (token.isLogin()) {

			ApiRequest.accountUserGiftCash.request(getActivity());

		} else {
			AccountLoginFragment.goWithConfirmResult(getActivity(), getActivity().getString(R.string.tip_need_login));
		}

	}

	@OnClick(R.id.id_account_logout)
	public void logout() {
		AccessTokenKeeper.clear(getActivity());
		initRequest(HandlerType.waitting);
		ViewUtils.toast(R.string.tip_logout_success);
	}

	@OnClick(R.id.id_account_consume_ticket)
	public void ticket() {
		MyPageHelper.accountTicket.showMyPage(getActivity());
	}

	@OnClick(R.id.id_account_invite)
	public void nurseJoinReady() {

		Bundle args = new Bundle();
		MyPageHelper.accountNurseJoinReady.showMyPage(getActivity(), args);

	}

	@OnClick(R.id.id_account_contact_service)
	public void service() {

		ViewUtils.confirm(getActivity(), "是否联系客服？", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int arg1) {
				String sereviceTel = OnlineParamUtils.get(Constants.ONLINE_PARAMS_KEY_SERVICE_TEL);
				PhoneUtils.phoneCall(getActivity(), sereviceTel);
			}
		});
	}

	@OnClick(R.id.id_account_share)
	public void share() {
		MyPageHelper.accountShare.showMyPage(getActivity());
	}

	@OnClick(R.id.id_account_feedback)
	public void feedback() {
		MyPageHelper.accountFeedback.showMyPage(getActivity());
	}

	public TextView getUserNameView() {
		return userNameText;
	}

	public TextView getTicketCountView() {
		return ticketCountText;
	}

	public ViewGroup getInviteLayout() {
		return inviteLayout;
	}

	public void setUser(AccountUser user) {
		this.user = user;
	}

}
